package com.powersercurity;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.jb.powersecurity.PowerSecurityController;
import android.jb.powersecurity.PowerSecurityController.ReceiveListener;
import android.jb.utils.Tools;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.ht380d4G_jar_demo.R;

public class PowerSecurityActivity extends Activity implements OnClickListener ,  ReceiveListener  {

	private static final byte[] checkSercurityState = new byte[] { (byte) 0xe9,
			(byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x10, (byte) 0xfb,
			(byte) 0xe6 };// 查询安全模块单元状态

	private static final byte[] checkVersion = new byte[] { (byte) 0xe9,
			(byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0xfc, (byte) 0xe7,
			(byte) 0xe6 };// 查询安全模块版本
	
	private static final byte[] checkCard1 = new byte[] { (byte) 0xe9, 0x00, 0x03, 0x00, 0x24, 0x01, 0x11, (byte) 0xe6};
	private static final byte[] checkCard2 = new byte[] { (byte) 0xe9, 0x00, 0x03, 0x00, 0x24, 0x02, 0x12, (byte) 0xe6 };
	private static final byte[] checkCard3 = new byte[] { (byte) 0xe9, 0x00, 0x02, 0x00, 0x15, 0x00, (byte) 0xe6 };
//	private static final byte[] checkCard3 = new byte[] { (byte)0xe9, 0x00, 0x02, 0x00, 0x29, 0x14, (byte)0xe6 }; 获取操作员状态

	private PowerSecurityController pSamCon;
	private int pro = -1;
	private static int PRO_READ_CARD1 = 1;
	private static int PRO_READ_CARD2 = 2;
	private static int PRO_READ_CARD3 = 3;
	private static int PRO_CheckSercurityState = 4;
	private static int PRO_CheckVersion = 5;
	
	private boolean isRead = false;//上电时模块会有数据返回，用于此时不接受显示数据
	private Button btn_checkSecurityState , btn_checkVersion , btn_checkCard1  , btn_checkCard2 , btn_checkCard3;
	private TextView tv_show,card_show;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1001:
				String text = (String)msg.obj;
				tv_show.setText(text);
				break;

			case 1002:
				String tex2t = (String)msg.obj;
				tv_show.setText(tex2t);
				if(i<50){
					isRead = true;
					if (pSamCon != null) {
						pro = PRO_READ_CARD3;
						pSamCon.writeCommand(checkCard3);
					}
					i++;
				}
				break;
			case 1003:
				String text3 = (String)msg.obj;
				card_show.setText(text3);
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_powersecurity);

		initView();
	}

	private void initView() {
		btn_checkSecurityState = (Button) findViewById(R.id.btn_checkSecurityState);
		btn_checkSecurityState.setOnClickListener(this);
		btn_checkVersion = (Button) findViewById(R.id.btn_checkVersion);
		btn_checkVersion.setOnClickListener(this);
		btn_checkCard1 = (Button) findViewById(R.id.btn_checkCard1);
		btn_checkCard1.setOnClickListener(this);
		btn_checkCard2 = (Button) findViewById(R.id.btn_checkCard2);
		btn_checkCard2.setOnClickListener(this);
		btn_checkCard3 = (Button) findViewById(R.id.btn_checkCard3);
		btn_checkCard3.setOnClickListener(this);
		tv_show = (TextView) findViewById(R.id.tv_show);
		card_show = (TextView) findViewById(R.id.card_show);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		sendBroadcast(new Intent("ReleaseCom"));
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pSamCon = PowerSecurityController.getInstance();

		try {
			pSamCon.PowerSecurity_Open(this , PowerSecurityActivity.this);
			// pSamCon.resetSam(4, psam_sam_type_sp.getSelectedItemPosition());
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		isRead = false;
		if (pSamCon != null)
			try {
				pSamCon.PowerSecurity_Close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		sendBroadcast(new Intent("ReLoadCom"));
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}
	
	int i = 0;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_checkSecurityState:
			isRead = true;
			if (pSamCon != null) {
				pro = PRO_CheckSercurityState;
				pSamCon.writeCommand(checkSercurityState);
			}
			break;
			
		case R.id.btn_checkVersion:
			isRead = true;
			if (pSamCon != null) {
				pro = PRO_CheckVersion;
				pSamCon.writeCommand(checkVersion);
			}
			break;
			
		case R.id.btn_checkCard1:
			isRead = true;
			if (pSamCon != null) {
				pro = PRO_READ_CARD1;
				pSamCon.writeCommand(checkCard1);
			}
			break;
			
		case R.id.btn_checkCard2:
			isRead = true;
			i = 0;
			if (pSamCon != null) {
				pro = PRO_READ_CARD2;
				pSamCon.writeCommand(checkCard2);
			}
			break;

		case R.id.btn_checkCard3:
			isRead = true;
			i = 0;
			if (pSamCon != null) {
				pro = PRO_READ_CARD3;
				pSamCon.writeCommand(checkCard3);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onReceive(byte[] data) {
		// TODO Auto-generated method stub
		if(!isRead)
			return;
		Message message = Message.obtain();
		message.what = 1001;
		if (null != data && data.length > 0) {
//			tv_show.setText(Tools.bytesToHexString(data));
			System.out.println("onReceive11:"+Tools.bytesToHexString(data)+" data: "+data[4]+" "+data[5]);
			if(pro == PRO_READ_CARD3){
				if(data[4] == (byte)0x95 && data[5] == 0x00 && data[6] == 0x64){
					message.obj = getResources().getString(R.string.power_synchronize_success) + "\r\n" + Tools.bytesToHexString(data);
				}else {
					message.what = 1002;
					message.obj = getResources().getString(R.string.info_abnormal) + "\r\n" + Tools.bytesToHexString(data);
					handler.sendMessage(message);
					return;
				}
				pro = -1;
			} else if(pro == PRO_READ_CARD1){
				if(data[4] == (byte)0xA4 && data[5] == 0x00){
					message.obj = getResources().getString(R.string.power_sercurity_success) + "\r\n" + Tools.bytesToHexString(data);
				}else {
					message.obj = getResources().getString(R.string.info_abnormal) + "\r\n" + Tools.bytesToHexString(data);
				}
				pro = -1;
			} else if(pro == PRO_READ_CARD2){
				if(data[4] == (byte)0xA4 && data[5] == 0x00){
					message.obj = getResources().getString(R.string.power_sercurity_success) + "\r\n" + Tools.bytesToHexString(data);
				}else {
					message.obj = getResources().getString(R.string.info_abnormal) + "\r\n" + Tools.bytesToHexString(data);
				}
				pro = -1;
			} else if(pro == PRO_CheckSercurityState) {
				if(data[4] == (byte)0x90 && data[5] == 0x00){
					message.obj = getResources().getString(R.string.iccard_r_9000) + "\r\n" + Tools.bytesToHexString(data);
				}else {
					message.obj = getResources().getString(R.string.info_abnormal) + "\r\n" + Tools.bytesToHexString(data);
				}
				pro = -1;
			} else if(pro == PRO_CheckVersion) {
				if(data[4] == (byte)0x7c && data[5] == 0x00){
					message.obj = getResources().getString(R.string.iccard_r_9000) + "\r\n" + Tools.bytesToHexString(data);
				}else {
					message.obj = getResources().getString(R.string.info_abnormal) + "\r\n" + Tools.bytesToHexString(data);
				}
				pro = -1;
			} else {
				message.obj = Tools.bytesToHexString(data);
			}
			
		} else {
			message.obj = "No Message";
//			tv_show.setText("无数据返回");
		}
		handler.sendMessage(message);
	}
	
	public void showCard(int card1,int card2){
		Message messageCard = Message.obtain();
		if(card1==49&&card2==49){
			messageCard.what = 1003;
			messageCard.obj=getResources().getString(R.string.nocard1)+getResources().getString(R.string.nocard2);
		}else if(card1==49&&card2==48){
			messageCard.what = 1003;
			messageCard.obj=getResources().getString(R.string.nocard1)+getResources().getString(R.string.hascard2);
		}else if(card1==48&&card2==49){
			messageCard.what = 1003;
			messageCard.obj=getResources().getString(R.string.hascard1)+getResources().getString(R.string.nocard2);	
		}else if(card1==48&&card2==48){
			messageCard.what = 1003;
			messageCard.obj=getResources().getString(R.string.hascard1)+getResources().getString(R.string.hascard2);	
		}
		handler.sendMessage(messageCard);
	}
}
